module.exports = function ($resource, $state, $http, $q, FileUploader, $modal, toaster) {
  
	return {

		restrict: 'AE',
		template: require('../views/projectlist.html'),
		replace: true,
		scope: {
			'saleobj': '=',
			'funobj': '=',
			'baseinfo': '=',
			'util': '=',
			'productflag' : '='
		},
		link: function (scope, elements, attrs) {
        /* 分页
      * ========================================= */
      scope.maxSize = 5;             //最多显示多少个按钮
      scope.bigCurrentPage = 1;      //当前页码
      scope.itemsPerPage = 10         //每页显示几条
      scope.obj={};
      scope.d={};

      scope.pageChanged = function () {
          var para = {
              pageNo:scope.bigCurrentPage,
              pageSize:scope.itemsPerPage,
          };
          $resource('/api/as/goc/reviewCompany/getCompanyId', {}, {}).
          save({}, function(res){			
              if(res.errcode === 0 || res.errcode === 10003){
                  scope.b=res.data;									
                  console.log(res);
                  console.log('上面是企业company_id') 
              }else{
                  alert(res.errmsg);
              }
          });
          $resource('/api/as/goc/reviewCompanyProject/findInfoList', {}, {}).
          save(para, function(res){			
              if(res.errcode === 0 || res.errcode === 10003){
                  scope.a=res.data.results;					
                  console.log(scope.a);
                  console.log('上面是企业项目列表返回值') 
                  scope.bigTotalItems = res.data.totalRecord;

              }else{
                  alert(res.errmsg);
              }
          });
      };
      scope.pageChanged();

      scope.search=function(){
          var dic = {
              pageNo:scope.currentPage,
              pageSize:scope.itemsPerPage,
          }
          $resource('', {}, {}).save(dic, function(res){
              if(res.errcode === 0 || res.errcode === 10003){
                  scope.a=res.data.results;
                  console.log(scope.a);
                  scope.totalItems = res.data.totalRecord;
              }else{
                  alert(res.errmsg);
              }
          });
      }


      // scope.add = function () {
      //     scope.item = scope.a;			
      //     var modalInstance = $modal.open({
      //       template: require('../views/addproject.html'),
      //       controller: 'addproject',
      //       size: 'lg',
      //       resolve: {
      //         items: function () {
      //           return scope.item;
      //         }
      //       }
      //     });
      //     modalInstance.result.then(function (showResult) {	
      //         scope.pageChanged();

      //     });
      // }

      scope.completeinfo = function () {
          $state.go('app.completeinfo',{'company_id':scope.b.company_id})
      }

      scope.toinfo= function (index) {
          scope.item = scope.a[index];	
          scope.flag = 'edit';		
          var modalInstance = $modal.open({
            template: require('../views/projectinfo.html'),
            controller: 'projectinfo',
            size: 'lg',
            resolve: {
              items: function () {
                return scope.item;
              },
              flag: function () {
                return scope.flag;
              }
            }
          });
          modalInstance.result.then(function (showResult) {	
              scope.pageChanged();
          });
      }

      scope.edit= function (index) {
          scope.item = scope.a[index];
          scope.flag = 'edit';			
          var modalInstance = $modal.open({
            template: require('../views/addproject.html'),
            controller: 'addproject',
            size: 'lg',
            resolve: {
              items: function () {
                return scope.item;
              },
              flag: function () {
                return scope.flag;
              }
            }
          });
          modalInstance.result.then(function (showResult) {	
              scope.pageChanged();
          });
      }

      scope.progress= function (index) {
          scope.item = scope.a[index];			
          var modalInstance = $modal.open({
            template: require('../views/project_progress.html'),
            controller: 'project_progress',
            size: 'lg',
            resolve: {
              items: function () {
                return scope.item;
              }
            }
          });
          modalInstance.result.then(function (showResult) {	
              scope.pageChanged();
          });
      }

      scope.delete= function (index) {    
          console.log(scope.a[index].project_code) ;          
          if(confirm('是否废弃该项目？')){
            $resource('/api/as/goc/reviewCompanyProject/delete', {}, {}).save({'project_code':scope.a[index].project_code}, function(res){			              
                  if(res.errcode === 0 || res.errcode === 10003){
              alert('废弃成功')			
              scope.pageChanged();
                  }else{
                      alert(res.errmsg);
                  }
              });
          
          }		
      }

      scope.apply= function (index) {    
          console.log(scope.a[index].project_code) ;          
          if(confirm('是否申请项目？')){
          $resource('/api/ac/goc/reviewCompanyProjectService/updateApply', {}, {}).save({'project_code':scope.a[index].project_code}, function(res){			              
                if(res.errcode === 0 || res.errcode === 10003){
            alert('申请成功')			
            scope.pageChanged();
                }else{
                    alert(res.errmsg);
                }
            });
        
          }		
          
      }

      scope.reapply= function (index) {    
          console.log(scope.a[index].project_code) ;          
          if(confirm('是否重新申请项目？')){
          $resource('/api/as/goc/reviewCompanyProject/updateApplyRe', {}, {}).save({'project_code':scope.a[index].project_code}, function(res){			              
                if(res.errcode === 0 || res.errcode === 10003){
            alert('重新申请成功')			
            scope.pageChanged();
                }else{
                    alert(res.errmsg);
                }
            });
        
          }		
          
      }
      // 监听
      scope.$watch('productflag', function(){
				scope.pageChanged();
			}, true)

    }
	}
}